package org.example.mulitithreading.ex6;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> ft1 = new FutureTask<>(() -> {
            Thread.sleep(5000);
            System.out.println("called1方法执行了");
            return "call方法返回值";
        });
        FutureTask<String> ft2 = new FutureTask<>(() -> {
            Thread.sleep(3000);
            System.out.println("called2方法执行了");
            return "call2方法返回值";
        });

        System.out.println("提交任务之前 " + getStringDate());
        new Thread(ft1).start();
        new Thread(ft2).start();
        System.out.println("提交任务之后 " + getStringDate());

        System.out.println("开始获取第一个返回值 " + getStringDate());
        System.out.println("获取返回值: " + ft1.get());
        System.out.println("获取第一个返回值结束，开始获取第二个返回值 " + getStringDate());
        System.out.println("获取返回值2: " + ft2.get());
        System.out.println("获取第二个返回值结束 " + getStringDate());
    }

    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        return formatter.format(currentTime);
    }
}
